package com.fourth_groups.shopping121store.ui.cart.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.fourth_groups.shopping121store.R;

public class CartAlreadyFragment extends Fragment {

    private String url = "http://m.121dian.com/cart";
    private WebView mWv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_cart_web, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mWv = (WebView) view.findViewById(R.id.wv);

        assert mWv != null;
        //用来对WebView进行设置
        WebSettings settings = mWv.getSettings();
        //支持javascript脚本功能
        settings.setJavaScriptEnabled(true);
        //是否支持缩放
        settings.setSupportZoom(true);
        //显示缩放控制器
        settings.setDisplayZoomControls(true);
//        加载url地址
        mWv.loadUrl(url);

        //修改默认的浏览器
        mWv.setWebViewClient(new WebViewClient() {
            //修改默认的浏览器
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        //处理fragment中的放回键
        mWv.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK ) {
                        //这里处理返回键事件
                        if (mWv!=null&&mWv.canGoBack()){
                            mWv.goBack();//后退
                        }

                    }
                }
                return false;
            }
        });


    }

    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
                    //这里处理返回事件
                }
                return false;
            }
        });
    }

}
